<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\admin\model\Admin as AdminModel;

/**
 * 管理员列表
 */
class Admin extends AdminBase
{
    /**
     * 用户列表
     */
    public function index($limit='10')
    {
        $model = new AdminModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $model = $model->where('name|username|mobile','like','%'.$param['keyword'].'%');
        }
        $lists = $model->order('sort_order desc,id desc')->paginate($limit);
        $this->result(compact('lists'));
    }

    /**
     * 编辑用户状态
     */
    public function edit_status()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            AdminModel::update($param,['id'=>$param['id']]);
            $this->result([],0,'操作成功');
        }
    }

    /**
     * 查看用户列表
     */
    public function get_list()
    {
        $list = AdminModel::where('status',1)->select();
        $this->result(compact('list'));
    }

    /**
     * 查看用户信息
     */
    public function userinfo($id='')
    {
        if(empty($id)){
            $id = AdminId();
        }
        $data = AdminModel::find($id);
        $this->result($data);
    }

    /**
     * 修改用户
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['password'])) {
                unset($param['password']);
            }
            // 角色重组
            foreach ($param['roles'] as $k=>$r){
                $roles[$k] = $r['id'];
            }
            $param['roles'] = $roles;
            $model = new AdminModel();
            if(!empty($param['id'])){
                unset($param['create_time']);
                unset($param['password']);
                $model = AdminModel::find($param['id']);
            }
            $result = $model->save($param);
            $this->result('',0,"操作成功");
        }
    }

    /**
     * 删除用户
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //判断是否主管理员
            if($param['id'] == 1){
                $this->result('',1,"主管理员不允许删除");
            }else{
                AdminModel::destroy($param['id']);
                $this->result('',0,"删除成功");
            }
        }
    }

    /**
     * 批量删除
     */
    public function batch()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //判断是否主管理员
            AdminModel::destroy($param['data']);
            $this->result('',0,"删除成功");
        }
    }

    /**
     * 重置密码
     */
    public function reset_password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            AdminModel::update($param,['id'=>$param['id']]);
            $this->result('',0,"重置成功");
        }
    }

    /**
     * 修改个人密码
     */
    public function edit_password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // 验证条件
            empty($param['oldPassword']) && $this->result('',1,"请输入旧密码");
            empty($param['password']) && $this->result('',1,"请输入新密码");
            //查询旧密码
            $admin = AdminModel::where('id', AdminId())->find();
            $admin['password'] != md5($param['oldPassword']) && $this->result('',1,"旧密码错误");
            $data = ['password' => $param['password']];

            $model = new AdminModel();
            $model = AdminModel::find(AdminId());
            $model->save($data);
            $this->result('',0,"修改成功");
        }
    }

    
}